package com.lwj.mallchat.common.common.thread;

import lombok.AllArgsConstructor;

import java.util.concurrent.ThreadFactory;

/**
 * @author lwj
 * @ClassName MyThreadFactory
 * @date 2024/10/18 17:15
 */
@AllArgsConstructor
public class MyThreadFactory implements ThreadFactory {

    private static final MyUncaughtExceptionHandlerThread MY_UNCAUGHT_EXCEPTION_HANDLER_THREAD = new MyUncaughtExceptionHandlerThread();
    private ThreadFactory threadFactory;

    @Override
    public Thread newThread(Runnable r) {

        Thread thread = threadFactory.newThread(r);//执行spring创建线程自己的创建逻辑

        //额外装饰线程的逻辑
        thread.setUncaughtExceptionHandler(MY_UNCAUGHT_EXCEPTION_HANDLER_THREAD);

        return thread;
    }
}
